import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '../store'
import { MessageBox } from 'element-ui'

Vue.use(VueRouter)
const router = new VueRouter({
	mode: 'history',
	base : '/otc/',
  	// base: process.env.BASE_URL,	
  	routes  : [
		{
			path : '/',
			component : () => import('@v/Index')
		},
		{//登录页
			path : '/login',
			component : () => import('@v/Login')
		},
		{//忘记密码页
			path : '/forget-pwd',
			component : () => import('@v/ForgetPwd')
		},
		{//注册页面
			path : '/reg',
			component : () => import('@v/Reg')
		},
		{//用户中心
			path : '/user-center',
			component : () => import('@v/user/UserCenter'),
			meta : {
				needLogin :true
			}
		},
		{//用户中心->用户信息
			path : '/user-info/:id?',
			component : () => import('@v/user/UserInfo'),
			meta : {
				needLogin :true
			}
		},
		{//修改手机号码
			path : '/change-telephone',
			component : () => import('@v/user/ChangeTelephone'),
			meta : {
				needLogin :true
			}
		},
		{//修改郵箱
			path : '/change-email',
			component : () => import('@v/user/ChangeEmail'),
			meta : {
				needLogin :true
			}
		},
		{//绑定邮箱
			path : '/bind-email',
			component : () => import('@v/user/BindEmail'),
			meta : {
				needLogin :true
			}
		},
		{//修改登录密码
			path : '/fixed-login-pwd',
			component : () => import('@v/user/FixedLoginPwd'),
			meta : {
				needLogin :true
			}
		},
		{//用户中心—>设置资金密码
			path : '/set-trade-pwd',
			component : () => import('@v/user/SetTradePwd'),
			meta : {
				needLogin :true
			}
		},
		{//用户中心->修改资金密码
			path : '/change-trade-pwd',
			component : () => import('@v/user/ChangeTradePwd'),
			meta : {
				needLogin :true
			}
		},
		{//用户中心->重置资金密码
			path : '/reset-trade-pwd',
			component : () => import('@v/user/ResetTradePwd'),
			meta : {
				needLogin :true
			}
		},
		{//用户中心->我的广告
			path :'/my-ads',
			component : () => import('@v/user/myAds'),
			meta : {
				needLogin :true
			}
		},
		{//我的订单
			path :'/my-order',
			component : () => import('@v/user/MyOrder'),
			meta : {
				needLogin :true
			}
		},
		{//信任管理
			path :'/trust-manage',
			component : () => import('@v/user/TrustManage'),
			meta : {
				needLogin :true
			}
		},
		{//我要买入
			path : '/buy-bitcoin',
			component : () => import('@v/trade/BuyBitcoin'),
			meta : {
				needLogin :true
			}
		},
		{//我要出售
			path : '/sell-bitcoin',
			component : () => import('@v/trade/SellBitcoin'),
			meta : {
				needLogin :true
			}
		},
		{//发布广告
			path : '/new-ad',
			component : () => import('@v/ads/newAd'),
			meta : {
				needLogin :true
			}
		},
		{//购买-出售广告详情
			path : '/trade-ad-detail/:tradeType/:id',
			component : () => import('@v/trade/TradeAdDetail'),
			meta : {
				needLogin :true
			}
		},
		{//兑换广告详情
			path : '/exchange-ad-detail/:id',
			component : () => import('@v/trade/ExchangeAdDetail'),
			meta : {
				needLogin :true
			}

		},
		{//礼品卡交易
			path : '/grift-card-trade',
			component : () => import('@v/trade/GiftCardTrade')
		},
		//认证相关
		{//选择国家以及证件类型
			path : '/auth-country',
			component : () => import('@v/auth/selectCountry'),
			meta : {
				needLogin :true
			}
		},
		{//姓名及身份证
			path : '/name-auth',
			component : () => import('@v/auth/nameAuth'),
			meta : {
				needLogin :true
			}
		},
		{//广告详情
			path : '/ad-detail/:id/:adType',
			component : () => import('@v/ads/AdDetail'),
			meta : {
				needLogin :true
			}
		},
		{//聊天界面
			path : '/chat/:id/:ad_id',
			component : () => import('@v/common/Chat'),
			meta : {
				needLogin :true
			}
		},
		{//我的钱包
			path : '/my-wallet',
			component : () => import('@v/user/Wallet'),
			meta : {
				needLogin :true
			}
		},
		//账户详情
		{
			path : '/acount-detail',
			component :() => import('@v/user/AccountDetail'),
			meta :{
				needLogin :true
			}
		},
		//兑换广告列表
		{
			path : '/exchange',
			component :() => import('@v/trade/Exchange'),
			meta :{
				needLogin :true
			}
		},
		{
			path : '/content/:id',
			component :() => import('@v/common/Content'),
			meta :{
			
			}
		},
		{//重定向其他路由到首页
			path : '*',
			redirect : to => {
				return '/'
			}
		}
	]
})


router.beforeEach((to, from, next) => {
	if (to.meta.needLogin && !store.state.userInfo.user_id) {
		MessageBox({
			title : '提示',
			message : '請先登錄',
			callback : action => {
				router.replace('/login')
			}
		})
	} else {
		next()
	}
})

export default router
